package com.xs.designs.strategy.factory.template_method.controller;

import com.xs.designs.strategy.factory.template_method.I.PaymentStrategy;
import com.xs.designs.strategy.factory.template_method.II.AliPay;
import com.xs.designs.strategy.factory.template_method.II.UnionPay;
import com.xs.designs.strategy.factory.template_method.II.WeChatPay;
import com.xs.designs.strategy.factory.template_method.IIII.PaymentStrategyFactory;
import com.xs.designs.strategy.factory.template_method.IIIII.AbstractPaymentStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PayController {

    @Autowired
    PaymentStrategyFactory paymentStrategyFactory;

    @RequestMapping("pay")
    public void pay(int payType, double amount) {
        PaymentStrategy paymentStrategy;
        switch (payType) {
            case 1:
                paymentStrategy=new WeChatPay();
                break;
            case 2:
                paymentStrategy=new AliPay();
                break;
            case 3:
                paymentStrategy=new UnionPay();
                break;
            default:
                throw new IllegalArgumentException("无效的支付类型");
        }
        paymentStrategy.pay(amount);
    }


    @RequestMapping("pay2")
    public void payStrategy(int payType, double amount) {
        AbstractPaymentStrategy paymentStrategy  = paymentStrategyFactory.getPaymentStrategy(payType);
        paymentStrategy.commonPay(amount);
    }
}
